package org.sky.wall.visual

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import org.sky.wall.visual.adapter.ListAdapter
import org.sky.wall.visual.databinding.ActivityMainBinding
import org.sky.wall.visual.service.WallpaperService
import org.sky.wall.visual.ui.PreviewActivity
import org.sky.wall.visual.ui.VideoActivity


class MainActivity : AppCompatActivity() {
	private lateinit var bin :ActivityMainBinding
	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		bin = ActivityMainBinding.inflate(layoutInflater)
		setContentView(bin.root)
	
		setAdapterData()
		initServices()
		initStrat()
	}
	
	private fun initStrat() {
		bin.camera.setOnClickListener {
			Intent(this,PreviewActivity::class.java).also {
				startActivity(it)
			}
		}
		bin.image.setOnClickListener {
			Intent(this,VideoActivity::class.java).also {
				startActivity(it)
			}
		}
	}
	
	private fun initServices() {
	bin.switch1.setOnCheckedChangeListener { _, isChecked ->
		if (isChecked) {
			
			val serviceIntent = Intent(this, WallpaperService::class.java).apply {
				putExtra("interval", 15_000L)
			}
			
			startService(serviceIntent)
			
		} else {
			stopService(Intent(this, WallpaperService::class.java));
		}
	}
	}
	
	private fun setAdapterData() {
		
		val list =  mutableListOf<Int>().apply {
			for (i in 1..27) {
				val resourceId = resources.getIdentifier("a$i", "drawable", packageName)
				if (resourceId != 0) {
					add(resourceId)
				}
			}
		}
		bin.rcv.apply {
			adapter = ListAdapter(this@MainActivity,list)
			layoutManager = GridLayoutManager(this@MainActivity, 3)
		}
	}
	
	
	
}